/**
 * @ProjectName:Backend
 * @Title: GlobalServiceImpl
 * @Package com.phrp.service.impl
 * @Description: 全局服务接口实现类
 * @author SuShuChao
 * @date 2024/4/21 10:30
 * @version V1.0
 * Copyright (c) 2024, 2034279227@qq.com All Rights Reserved.
 */
package com.phrp.service.impl;

import com.phrp.dao.GlobalDao;
import com.phrp.service.GlobalService;
import com.phrp.util.oss.UploadFile;
import com.phrp.util.returnResult.ResponseResult;
import com.phrp.util.returnResult.ReturnInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.LinkedList;

@Service
public class GlobalServiceImpl implements GlobalService {

    @Autowired
    private GlobalDao globalDao;

    @Autowired
    private UploadFile upFile;

    private static final String[] IMAGE_TYPE = new String[]{".jpg", ".jpeg", ".gif", ".png"};

    private static final String[] FILE_TYPE = new String[]{".doc",".docx"};

    /**
     * 查询字典表的数据值
     * @param tableName
     * @param fieldName
     * @return
     */
    @Override
    public ResponseResult getDictionaryValue(String tableName, String fieldName) {
        // 通过表名和列名去查询数据库中的信息
        LinkedList<HashMap<String,String>> list = globalDao.selectValue(tableName,fieldName);

        return new ResponseResult(ReturnInfoEnum.SUCCESS,list);
    }

    /**
     * 获取图片信息并返回
     * @param file
     * @return
     */
    @Override
    public ResponseResult uploadImg(MultipartFile file) {
        String s = upFile.uploadImg(IMAGE_TYPE, file);

        if(s == "false"){
            return ResponseResult.error("400","未选择文件或文件格式错误~");
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS,s);
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @Override
    public ResponseResult uploadFile(MultipartFile file) {
        String s = upFile.uploadFile(FILE_TYPE, file);

        if(s == "false"){
            return ResponseResult.error("400","未选择文件或文件格式错误~");
        }
        return new ResponseResult(ReturnInfoEnum.SUCCESS,s);
    }
}
